package Echart

import (
	"WebSvr/modules/common"
	"WebSvr/modules/mysql"
	"fmt"

	"github.com/astaxie/beego"
)

type S2CPcInfoJson struct {
	S2CBaseJson
	Grid   []Grid_sub          `json:"grid"`
	XAxis  []PcInfo_xAxis_sub  `json:"xAxis"`
	YAxis  []PcInfo_yAxis_sub  `json:"yAxis"`
	Series []PcInfo_series_sub `json:"series"`
}

type Grid_sub struct {
	Left   int32  `json:"left"`
	Right  int32  `json:"right"`
	Top    string `json:"top"`
	Height string `json:"height"`
}

type PcInfo_xAxis_sub struct {
	GridIndex   int32  `json:"gridIndex"`
	Type        string `json:"type"`
	BoundaryGap bool   `json:"boundaryGap"`
	AxisLine    `json:"axisLine"`
	Data        []string `json:"data"`
	Position    string   `json:"position"`
}

type AxisLine struct {
	OnZero bool `json:"onZero"`
}

type PcInfo_yAxis_sub struct {
	GridIndex int32  `json:"gridIndex"`
	Name      string `json:"name"`
	Type      string `json:"type"`
	Max       int32  `json:"max"`
	Inverse   bool   `json:"inverse"`
}

type PcInfo_series_sub struct {
	Name           string    `json:"name"`
	Type           string    `json:"type"`
	XAxisIndex     int32     `json:"xAxisIndex"`
	YAxisIndex     int32     `json:"yAxisIndex"`
	SymbolSize     int32     `json:"symbolSize"`
	HoverAnimation bool      `json:"hoverAnimation"`
	Data           []float64 `json:"data"`
}

func (pS2CJson *S2CPcInfoJson) getS2CData(pC2Sjson *common.C2SJson, table string) error {
	//legond
	legondSlice := []string{"cpu", "mem"}
	pS2CJson.Legond.Data = legondSlice
	pS2CJson.Legond.X = "left"
	//S2CBaseJson dataZoom
	var pc_zoom_cpu DataZoom_sub
	pc_zoom_cpu.Type = ""
	pc_zoom_cpu.Show = true
	pc_zoom_cpu.Realtime = true
	pc_zoom_cpu.Start = 0
	pc_zoom_cpu.End = 100
	pc_zoom_cpu.XAxisIndex = 0
	var pc_zoom_mem DataZoom_sub
	pc_zoom_mem.Type = "inside"
	pc_zoom_mem.Show = true
	pc_zoom_mem.Realtime = true
	pc_zoom_mem.Start = 0
	pc_zoom_mem.End = 100
	pc_zoom_mem.XAxisIndex = 1

	pS2CJson.DataZoom = []DataZoom_sub{pc_zoom_cpu, pc_zoom_mem}

	//Grid
	var gridSub_cpu Grid_sub
	gridSub_cpu.Left = 50
	gridSub_cpu.Right = 50
	gridSub_cpu.Top = "10%"
	gridSub_cpu.Height = "35%"

	var gridSub_mem Grid_sub
	gridSub_mem.Left = 50
	gridSub_mem.Right = 50
	gridSub_mem.Top = "55%"
	gridSub_mem.Height = "35%"
	pS2CJson.Grid = []Grid_sub{gridSub_cpu, gridSub_mem}

	//xAxis
	var pc_xAxisub_cpu PcInfo_xAxis_sub
	pc_xAxisub_cpu.GridIndex = 0
	pc_xAxisub_cpu.Type = "category"
	pc_xAxisub_cpu.AxisLine.OnZero = true
	pc_xAxisub_cpu.Position = "bottom"

	xAxisSlice, xAxisErr := createxAxisSubData(pC2Sjson.SelType, pC2Sjson.StartTime, pC2Sjson.EndTime)
	if xAxisErr != nil {
		beego.Error("createxAxisSubData err:%v", xAxisErr)
		return xAxisErr
	}
	pc_xAxisub_cpu.Data = xAxisSlice

	var pc_xAxisub_mem PcInfo_xAxis_sub
	pc_xAxisub_mem.GridIndex = 1
	pc_xAxisub_mem.Type = "category"
	pc_xAxisub_mem.AxisLine.OnZero = true
	pc_xAxisub_mem.Position = "top"
	pc_xAxisub_mem.Data = xAxisSlice
	pS2CJson.XAxis = []PcInfo_xAxis_sub{pc_xAxisub_cpu, pc_xAxisub_mem}
	//yAxis
	var pc_yAxis_cpu PcInfo_yAxis_sub
	pc_yAxis_cpu.GridIndex = 0
	pc_yAxis_cpu.Name = "cpu(%)"
	pc_yAxis_cpu.Type = "value"
	pc_yAxis_cpu.Max = 100
	pc_yAxis_cpu.Inverse = false

	var pc_yAxis_mem PcInfo_yAxis_sub
	pc_yAxis_mem.GridIndex = 1
	pc_yAxis_mem.Name = "mem(%)"
	pc_yAxis_mem.Type = "value"
	pc_yAxis_mem.Max = 100
	pc_yAxis_mem.Inverse = true
	pS2CJson.YAxis = []PcInfo_yAxis_sub{pc_yAxis_cpu, pc_yAxis_mem}
	//series
	series_len := len(pS2CJson.Legond.Data)
	pS2CJson.Series = make([]PcInfo_series_sub, series_len, series_len+1)
	var series_value_cpu PcInfo_series_sub
	series_value_cpu.Name = "cpu"
	series_value_cpu.Type = "line"
	series_value_cpu.XAxisIndex = 0
	series_value_cpu.YAxisIndex = 0
	series_value_cpu.SymbolSize = 8
	series_value_cpu.HoverAnimation = false

	/*按selType转对应数据  xAxisSlice pcSlice*/

	var series_value_mem PcInfo_series_sub
	series_value_mem.Name = "mem"
	series_value_mem.Type = "line"
	series_value_mem.XAxisIndex = 1
	series_value_mem.YAxisIndex = 1
	series_value_mem.SymbolSize = 8
	series_value_mem.HoverAnimation = true

	cpuData, memData, errPcData := mysql.QueryPCDataBySeltype(pC2Sjson.SelType, pC2Sjson.ServerName, table, pc_xAxisub_cpu.Data)
	if errPcData == nil {
		fmt.Printf("cpuData:%v\n", cpuData)
		fmt.Printf("memData:%v\n", memData)
		series_value_cpu.Data = cpuData[0:len(cpuData)]
		series_value_mem.Data = memData[0:len(memData)]
		fmt.Printf("series_value_cpu.Data:%v\n", series_value_cpu.Data)
	}
	pS2CJson.Series = []PcInfo_series_sub{series_value_cpu, series_value_mem}

	return nil

}
